﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;

namespace UTable.Objects.Shapes
{
    public class UPolygon : UShape
    {
        #region Dependency Properties

        public PointCollection Points
        {
            get { return (PointCollection)GetValue(PointsProperty); }
            set { SetValue(PointsProperty, value); }
        }

        public static readonly DependencyProperty PointsProperty =
            DependencyProperty.Register("Points", typeof(PointCollection), typeof(UPolygon));

        #endregion

        #region Override Methods

        protected override Geometry DefiningGeometry
        {
            get
            {
                StreamGeometry geometry = new StreamGeometry();
                geometry.FillRule = FillRule.EvenOdd;

                using (StreamGeometryContext context = geometry.Open())
                {
                    InternalDrawGeometry(context);
                }

                // Freeze the geometry for performance benefits
                geometry.Freeze();

                return geometry;
            }
        }

        private void InternalDrawGeometry(StreamGeometryContext context)
        {
            if (Points == null || Points.Count < 1) return;
            context.BeginFigure(Points[0], true, true);
            for (int i = 1; i < Points.Count; ++i)
            {
                context.LineTo(Points[i], true, true);
            }
        }

        #endregion
    }
}
